package cn.hpclub.server.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.tx.Tx;

@TableBind(tableName = "sync_data")
public class SyncData extends Model<SyncData>{
    private static final long    serialVersionUID = -8028841716035214650L;
    private static final Logger  logger           = LoggerFactory.getLogger(SyncData.class);
    public static final SyncData dao              = new SyncData();

    public static String         ID               = "id";
    public static String         TAG              = "tag";
    public static String         ORG_ID           = "path";

    @Before({ Tx.class })
    public void saveSyncData(String tag, String path, String digest, String fileName){
        SyncData data = findFirst("SELECT * FROM sync_data WHERE tag=?", tag);
        if(data != null){
            Db.update("DELETE FROM sync_data WHERE tag=?", tag);
        }
        Db.update("INSERT INTO sync_data(tag, path,digest,file_name) VALUES (?,?,?,?)", tag, path, digest, fileName);
        logger.debug(String.format("tag=%s,path=%s,digest=%s,fileName=%s", tag, path, digest, fileName));
    }

    public SyncData getSyncData(String tag){
        return findFirst("SELECT * FROM sync_data WHERE tag=?", tag);
    }

    public SyncData getSyncDataByDigest(String digest){
        return findFirst("SELECT * FROM sync_data WHERE digest=?", digest);
    }

}
